package com.company.admin.service.job.plan.school;

import com.company.admin.common.myException.BizException;
import com.company.admin.dao.plan.school.SchoolJobPlanDao;
import com.company.admin.domain.job.plan.school.SchoolJobPlanListResponse;
import com.company.admin.entity.plan.SchoolJobPlanEntity;
import com.company.admin.req.job.plan.school.SchoolJobPlanDeleteRequest;
import com.company.admin.req.job.plan.school.SchoolJobPlanListRequest;
import com.company.admin.req.job.plan.school.SchoolJobPlanRequest;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: myProject
 * @description: ${description}
 * @author: zhaochenglin
 * @create: 2019-03-03 17:03
 **/
@Service
public class SchoolJobPlanService {

    @Autowired
    private SchoolJobPlanDao schoolJobPlanDao;



    @Transactional
    public void add (SchoolJobPlanRequest request){
        if (request == null){
            throw new BizException(210, "请求实体不存在");
        }
        if (StringUtils.isBlank(request.getPlanAddress())){
            throw new BizException(210, "宣讲会的地址不可以为空");
        }
        if (request.getPlanTime() == null){
            throw new BizException(210, "宣讲会的时间不可以为空");
        }
        if (StringUtils.isBlank(request.getPlanSchool())){
            throw new BizException(210, "宣讲的学校不可以为空");
        }
        SchoolJobPlanEntity entity = req2Entity(request);
        schoolJobPlanDao.insert(entity);
    }

    private SchoolJobPlanEntity req2Entity(SchoolJobPlanRequest request){
        SchoolJobPlanEntity entity = new SchoolJobPlanEntity();
        entity.setCreateTime(new Date());
        entity.setPlanActive(request.getPlanActive());
        entity.setPlanAddress(request.getPlanAddress());
        entity.setPlanSchool(request.getPlanSchool());
        entity.setSchoolJobPlanId(request.getSchoolJobPlanId());
        entity.setUpdateTime(new Date());
        return entity;
    }

    @Transactional
    public void modify(SchoolJobPlanRequest request){
        if (request.getSchoolJobPlanId() == null){
            throw new BizException(210,"请选择要修改的校园宣讲");
        }
        schoolJobPlanDao.modify(req2Entity(request));
    }

    public SchoolJobPlanListResponse list(SchoolJobPlanListRequest request){
        if (request.getLength() == null || request.getOffset() == null){
            throw new BizException(210, "分页参数不完整");
        }
        SchoolJobPlanListResponse response = new SchoolJobPlanListResponse();
        Long totalCount = schoolJobPlanDao.totalCount();
        if (totalCount.equals(0l)){
            response.setTotalCount(totalCount);
            return response;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("offset", request.getOffset());
        map.put("length", request.getLength());
        List<SchoolJobPlanEntity> entities = schoolJobPlanDao.list(map);
        response.setTotalCount(totalCount);
        response.setDomains(entities);
        return response;
    }

    public void delete(SchoolJobPlanDeleteRequest request){
        if (CollectionUtils.isEmpty(request.getSchoolJobPlanIds())){
            throw new BizException(210, "请先选择要删除的校园宣讲会");
        }
        schoolJobPlanDao.delete(request.getSchoolJobPlanIds());
    }
}
